package com.chuan.thinkingjava.interview.look;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

public class TestLook {

    private static int i = 0;

    private static List<Integer> cache = new LinkedList<>();

    public static void main(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();
        lock.lock();

        try {
            for (int j = 0; j < 200; j++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        cache.add(incr());
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        Thread.sleep(100);
        List<Integer> collect = cache.stream().sorted().collect(Collectors.toList());
        System.out.println(collect);
    }

    private static int incr() {
        return ++i;
    }
}
